<?php
class RestaurantModel extends Model {
	/**
	 * poi云检索，获取用户周边一定范围的圆形区域内的餐厅列表数据
	 */
	public function getOutletArray($query){
		
		$poit = $query['pointlng'].",".$query['pointlat'];
		$list = mapNearby(array("location"=>$poit));
		
		//筛选餐厅
		$where['isdel']  = 2;
		$where['isrest'] = array('neq',1);
		$where['cityid'] = $query['city'];
		$data = M('Outlet')->where($where)->select();
		
		//遍历餐厅数组，将餐厅的详细数据赋给该数组
		foreach($list as $k=>$v){
			foreach($data as $m=>$n){
				if($v['outletid'] == $n['id']){
					$list[$k]['detail'] = $n;
				}
			}
			/*$map['id']      = $v['outletid'];
			$map['thumb']   = 2;
			$map['nofield'] = 'service,enservice,invoice,eninvoice,detail,endetail,address,enaddress,encontent,information,content';
			$data = sendRest("BDC/Outlet/detail",$map);
			
			if($data){
				if($data['data']['city'] == $query['cityid']){
					$list[$k]['detail'] = $data['data'];
				}else{
					unset($list[$k]);
				}
			}
			*/
		}
		return $list;
	}
	
	/**
	 * 根据百度地图获取的poi点，根据距离以及时间的远近进行筛选，分割成多个数组
	 * @param Array $list
	 * @param Array $map
	 */
	public function getRestaurantArray($list,$map){
	
		$config = F('Sysconfig');
		$query  = addressJsonDecode();
		$juli   = $config['sys'][$query['encity']];
		//$xishu  = $config['cfg']['xishu'];
		$xishu  = 1;
		$arr    = array();

		foreach($list as $k=>$v){
			
			if($map['cuisine']){
				$array = explode(',',$v['detail']['cuisine']);
				if(in_array($map['cuisine'], $array)){
					foreach($juli as $m=>$n){
						if( $n[0]<$v['distance'] && $v['distance']<$n[1] ){
								
							//是否减免服务费
							$exempt = $this->getDelivery($v['detail'],$n[3]);    //是否减免服务费标识
							$isgao  = $this->getOutletGao($v['detail'],$config); //判断是否属于高峰时段
								
							$arr[$m]['arr'][$k] = $list[$k];
							$arr[$m]['price']   = $n[3];                         //服务费
							$arr[$m]['arr'][$k]['distance'] = $v['distance'];
							$arr[$m]['arr'][$k]['exempt']  = $exempt['exempt'];  //是否减免服务费
							$arr[$m]['arr'][$k]['isgao']   = $isgao['isgao'];    //是否是城市高峰时段
							$arr[$m]['arr'][$k]['gaotime'] = $isgao['gaotime'];  //城市高峰时段延迟时间
	
							$time = ($n[2]+$v['detail']['yanshi'])*$xishu;
							$time = $isgao['isgao']==1?$time+$isgao['gaotime']:$time;
							$time = $this->getIntTime($time);
							$arr[$m]['time']    = $time;
							
							if($v['distance']>6000){
								$arr[$m]['more'] = 1;        //是否属于更多餐厅里
							}
							if($v['distance']>8000){
								$arr[$m]['far'] = 1;         //配送费是否大于50元
							}
						}
					}
				}
			}else{
				foreach($juli as $m=>$n){
					if( $n[0]<$v['distance']  &&  $v['distance']<=$n[1] ){
							
						//是否减免服务费
						$exempt = $this->getDelivery($v['detail'],$n[3]);    //是否减免服务费标识
						$isgao  = $this->getOutletGao($v['detail'],$config); //判断是否属于高峰时段
							
						$arr[$m]['arr'][$k] = $list[$k];
						$arr[$m]['price']   = $n[3];             //服务费
						$arr[$m]['arr'][$k]['distance'] = $v['distance'];
						$arr[$m]['arr'][$k]['exempt']   = $exempt['exempt']; //是否减免服务费
						$arr[$m]['arr'][$k]['isgao']    = $isgao['isgao'];   //是否是城市高峰时段
						$arr[$m]['arr'][$k]['gaotime']  = $isgao['gaotime']; //城市高峰时段延迟时间
	
						$time = ($n[2]+$v['detail']['yanshi'])*$xishu;
						$time = $isgao['isgao']==1?$time+$isgao['gaotime']:$time;
						$time = $this->getIntTime($time);
						$arr[$m]['time']    = $time;
						
						if($v['distance']>6000){
							$arr[$m]['more']    = 1;     //是否属于更多餐厅里
						}
						if($v['distance']>8000){
							$arr[$m]['far'] = 1;         //配送费是否大于50元
						}
					}
				}
			}
		}
		
		//dump($arr);exit;
		return $arr;
	}
	
	/**
	 * 判断是否属于高峰时段
	 * @param array $detail 餐厅详情
	 * @param array $config 系统配置
	 */
	public function getOutletGao($detail,$config){
		
		$cityid  = C('CITYID'); //系统配置
		$address = $cityid[$detail['city']]; 
		$timearr = $config['cfg']['gaofeng'][$address[0]];
		$now     = strtotime(date("H:i:s"));
		$isgao   = 2;
		
		if($timearr['start1']){
			$start = strtotime($timearr['start1']);
			$end   = strtotime($timearr['end1']);
			
			if($now>=$start&&$now<=$end){
				$isgao = 1;
			}
		}
		
		if($timearr['start2']){
			$start = strtotime($timearr['start2']);
			$end   = strtotime($timearr['end2']);
				
			if($now>=$start&&$now<=$end){
				$isgao = 1;
			}
		}
		return array("isgao"=>$isgao,"gaotime"=>$timearr['time']);
	}
	
	/**
	 * 判断是否开启减免运费，是否在减免运费时间段
	 * 1是2否
	 */
	public function getDelivery($detail,$price){
		
		$weekarr = array("7","1","2","3","4","5","6");  //星期数组
		$week    = $weekarr[date("w")];                 //获取到今天星期几
		$exempt  = 1;                                   //免费标识  1否2是
		$start   = strtotime(date("Y-m-d").' '.$detail['exemptbegin']);               //餐厅免费开始时间
		$end     = strtotime(date("Y-m-d").' '.$detail['exemptend']);                 //餐厅免费结束时间
		$now     = strtotime(date("Y-m-d H:i"));         //当前时间
		$eprice  = $detail['exemptprice'];               //需要减免的费用
		$serve   = $price;                               //减免后的费用初始化

		//如果开启了减免配送费
		if($detail['is_exempt']==1){
			//判断是否在星期几之内
			if($detail['week']){
				$arr = explode(',',$detail['week']);
				if(in_array($week,$arr)){
					if($now>=$start&&$now<=$end){
						$exempt = 2;
						$serve  = $price-$eprice;
					}
				}
			}
		}
		return array("exempt"=>$exempt,"sever"=>$serve);
	}
	
	/**
	 * 根据时间，获取到整数时间
	 * @param unknown_type $time
	 */
	public function getIntTime($time){
		
		$a = substr($time,-1);
		$b = substr($time,0,-1);
		
		if($a>=0&&$a<=5){
			$a = 5;
		}
		if(5<$a&&$a<=9){
			$a = 0;
			$b+=1;
		}
		
		return $b.$a;
	}
	
	//循环餐厅列表将菜系字符串转成数组(其他相应操作)
	public function setRestaurantArray($view){
		foreach ($view as $k=>$v){
			foreach($v['arr'] as $m=>$n){
				$view[$k]['arr'][$m]['detail']['cuisine'] = explode(",",$n['detail']['cuisine']);
		
				//餐厅等级
				$level = $view[$k]['arr'][$m]['detail']['level'];
				$view[$k]['arr'][$m]['detail']['level'] = $level*20;
		
				//餐厅开业时间和休息时间
				$start = date('Y-m-d').' '.$view[$k]['arr'][$m]['detail']['starthours'];
				$end   = date('Y-m-d').' '.$view[$k]['arr'][$m]['detail']['endhours'];
				
				$start = strtotime($start);
				$end   = strtotime($end);
				$now   = time();
				
				if($view[$k]['arr'][$m]['detail']['starthours1']){
					$start1 = date('Y-m-d').' '.$view[$k]['arr'][$m]['detail']['starthours1'];
					$end1   = date('Y-m-d').' '.$view[$k]['arr'][$m]['detail']['endhours1'];
						
					$start1 = strtotime($start1);
					$end1   = strtotime($end1);
						
					//如果开始时间大于当前时间并且结束时间小于当前时间，那么就是暂停营业时间
					if(($start1>$now||$end1<$now)&&($start>$now||$end<$now)){
						$view[$k]['arr'][$m]['detail']['close'] = "close";
					}
				}else{
					//如果开始时间大于当前时间并且结束时间小于当前时间，那么就是暂停营业时间
					if($start>$now||$end<$now){
						$view[$k]['arr'][$m]['detail']['close'] = "close";
					}
				}
			}
		}
		return $view;
	}
	
	/**
	 * 获取所有的菜系
	 * @return array
	 */
	public function getCuisine($query){
		
		$map['cityid'] = $query['cityid'];
		$data =  sendRest("BDC/Cuisine/view",$map);
		
		return $data['data'];
	}
	
	/**
	 * 获取最新消息
	 */
	public function getArticleList(){
		$model = M("Article");

		$map['isdel']   = 2;
		$map['checked'] = 2;
		$map['lang']    = getLang();
		$data = $model->where($map)->select();
		return $data;
	}
	
	/**
	 * 设置会员登录后的地址session，可以直接去到餐厅列表页面选择餐厅
	 */
	public function setUserSession($addressid){
		
		//获取地址详情
		$map['id'] = $addressid;
		$data = sendRest('RMS/Address/detail',$map,'post');
		
		if($data['status']==1){
			$map['pointlng'] = $data['data']['longitude'];
			$map['pointlat'] = $data['data']['latitude'];
			$map['addid']    = $addressid;
			$map['adds']     = $data['data']['address'];
			$map['enadds']   = $data['data']['address'];
			$map['cityid']   = $data['data']['city'];
			$map['siteid']   = $data['data']['addressid'];
			$map['city']     = getaddresscity($data['data']['city'],'zh-cn');
			$map['zhcity']   = getaddresscity($data['data']['city'],'zh-cn');
			$map['encity']   = getaddresscity($data['data']['city'],'en-us');
			
			addressJsonEncode($map);
		}
	}
	
}
?>